<template>
  <div class="search">
    <van-nav-bar fixed title="商品列表" left-arrow @click-left="$router.go(-1)" />

    <van-search
      readonly
      shape="round"
      background="#ffffff"
      :value="querySearch || queryName || '请输入搜索关键词'"
      show-action
    >
      <template #action>
        <van-icon class="tool" name="apps-o" />
      </template>
    </van-search>

    <!-- 排序选项按钮 -->
    <div class="sort-btns">
      <div class="sort-item">综合</div>
      <div class="sort-item">销量</div>
      <div class="sort-item">价格 </div>
    </div>

    <div class="goods-list" v-if="querySearch">
      <GoodsItem v-for="item in proList"  :item="item" :key="item.goods_id"></GoodsItem>
    </div>
    <div class="goods-list" v-else>
      <GoodsList v-for="item in bannerList"  :item="item" :key="item.id"></GoodsList>
    </div>
  </div>
</template>

<script>
import GoodsItem from '@/components/GoodsItem.vue'
import GoodsList from '@/components/GoodsList.vue'
import axios from 'axios';
export default {
  name: 'SearchIndex',
   data () {
    return {
      proList: [],
      bannerList: [],
      List: [],
      page:1,
    }
  },
  computed: {
     querySearch() {
      return this.$route.query.key;
    },
    queryName() {
      return this.$route.query.name;
    }
  },
  components: {
    GoodsItem,
    GoodsList
  },
   async created() {
    //  const res=await axios({
    //   url:'http://smart-shop.itheima.net/index.php?s=/api/page/detail'
    //  })
    //  this.proList=res.data.data.pageData.items[6].data
    const res=await axios({
      url:'http://smart-shop.itheima.net/index.php?s=/api/goods/list',
      params:{
       goodsName:this.$route.query.key,
       page:this.page,
      }
    })
    this.proList=res.data.data.list.data
    const res2=await axios({
        url:'http://pcapi-xiaotuxian-front-devtest.itheima.net/home/category/head'
    })
     this.List=res2.data.result
     const Index=this.List.findIndex(item=>item.name===this.$route.query.name)
     this.bannerList=this.List[Index].goods
  },
}
</script>

<style lang="less" scoped>
.search {
  padding-top: 46px;
  ::v-deep .van-icon-arrow-left {
    color: #333;
  }
  .tool {
    font-size: 24px;
    height: 40px;
    line-height: 40px;
  }

  .sort-btns {
    display: flex;
    height: 36px;
    line-height: 36px;
    .sort-item {
      text-align: center;
      flex: 1;
      font-size: 16px;
    }
  }
}

// 商品样式
.goods-list {
  background-color: #f6f6f6;
}
</style>